#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include "NO_ENTER.h"

#define NEW_PERSON_POINT            1
#define CLEAR_PERSON_OLD_POINT      0
#define RIGHT                       1
#define LEFT                        0

typedef struct Map
{
    char arr[10][100];
}Map;

typedef struct point
{
    int x;
    int y;
}point;

typedef struct person
{
    point head;
    point left_Hand;
    point right_Hand;
    point left_Leg;
    point right_Leg;
}Person;

//地图设置人的位置
void map_Draw_Person_PointSet(Map* map, Person* person, int choice)
{
    //其实这里只需要一个头坐标位置就可以推断出其他部位位置，但是这样写更易理解
    if(choice == NEW_PERSON_POINT)
    {
        map->arr[person->head.y][person->head.x] = 'o'; 
        map->arr[person->left_Hand.y][person->left_Hand.x] = '/';
        map->arr[person->right_Hand.y][person->right_Hand.x] = '\\';
        map->arr[person->left_Leg.y][person->left_Leg.x] = '|';
        map->arr[person->right_Leg.y][person->right_Leg.x] = '|';
    }
    else if(choice == CLEAR_PERSON_OLD_POINT)
    {
        map->arr[person->head.y][person->head.x] = ' '; 
        map->arr[person->left_Hand.y][person->left_Hand.x] = ' ';
        map->arr[person->right_Hand.y][person->right_Hand.x] = ' ';
        map->arr[person->left_Leg.y][person->left_Leg.x] = ' ';
        map->arr[person->right_Leg.y][person->right_Leg.x] = ' ';
    }
}
//给人物位置赋值
void person_Position_Set(Person* person, point new, int move_direction, int* person_direciton)
{
    person->left_Leg.x = new.x;
    person->left_Leg.y = new.y;
    if(move_direction == RIGHT)
    {
        if(*person_direciton == RIGHT)//人物朝向
        {
            person->head.x++;
        }
        else
        {
            *person_direciton = RIGHT;
            person->head.x += 2;
        }
        person->left_Hand.x++;
        person->right_Hand.x++;
        person->right_Leg.x++;
    }
    else if(move_direction == LEFT)
    {
        if(*person_direciton == LEFT)//人物朝向
        {
            person->head.x--;
        }
        else
        {
            *person_direciton = LEFT;
            person->head.x -= 2;
        }
        person->left_Hand.x--;
        person->right_Hand.x--;
        person->right_Leg.x--;
    }
}

//绘制地图
void draw_Map(Map* map)
{
    system("clear");
    for(int idx = 0; idx < 10; idx++)
    {
        for(int jdx = 0; jdx < 100; jdx++)
        {
            printf("%c", map->arr[idx][jdx]);
        }
        printf("\n");
    }
}

int main()
{   
    //地图
    Map map;
    for(int idx = 0; idx < 10; idx++)
    {
        for(int jdx = 0; jdx < 100; jdx++)
        {
            map.arr[idx][jdx] = ' ';
        }
    }
    //地板
    for(int i = 0; i < 100; i++)
    {
        map.arr[9][i] = '-';
    }
    //头坐标
    Person P;
    P.head.x = 1;
    P.head.y = 6;
    //左手坐标
    P.left_Hand.x = 0;
    P.left_Hand.y = 7;
    //右手坐标
    P.right_Hand.x = 1;
    P.right_Hand.y = 7;
    //左腿坐标
    P.left_Leg.x = 0;
    P.left_Leg.y = 8;
    //右腿坐标
    P.right_Leg.x = 1;
    P.right_Leg.y = 8;

    //初始化画人
    map_Draw_Person_PointSet(&map, &P, NEW_PERSON_POINT);
    draw_Map(&map);
    // printf("o\n/\\\n||\n");
    //printf("%d  %d\n", head.x, head.y);
    point Position = {0, 8};//这里左脚的坐标初始值一致
    int person_direction = RIGHT;
    while(1)
    {
        int move_direction = -1;
        char push = getch();
        if(push == 'd') //右移
        {
            move_direction = RIGHT;
            Position.x++;
        }
        else if(push == 'a') //左移
        {
            move_direction = LEFT;
            Position.x--;
        }

        //清除原来的人物
        map_Draw_Person_PointSet(&map, &P, CLEAR_PERSON_OLD_POINT);
        //设置人物新的位置
        person_Position_Set(&P, Position, move_direction, &person_direction);
        //设置地图中新的人物位置
        map_Draw_Person_PointSet(&map, &P, NEW_PERSON_POINT);
        //画图
        draw_Map(&map);
    }
}